package com.jlcloud.core.oauth2.utils;

import com.jlcloud.core.oauth2.service.OAuth2Client;
import com.jlcloud.core.oauth2.service.OAuth2User;
import com.jlcloud.core.secure.TokenInfo;
import com.jlcloud.core.secure.utils.SecureUtil;
import com.jlcloud.core.tool.support.Kv;

import static com.jlcloud.core.launch.constant.TokenConstant.*;

/**
 * OAuth2Util
 *
 * @author By: JlCloud <br>
 * @Package: com.jlcloud <br>
 * @CreateTime: 2024-12-03 21:45 <br>
 * @Copyright: 2021 www.jilianjituan.com Inc. All rights reserved. <br>
 * @Caution 注意：本内容仅限于冀联人力集团内部传阅，禁止外泄以及用于其他的商业目的 <br>
 */
public class OAuth2Util extends SecureUtil {

	/**
	 * 创建accessToken
	 *
	 * @param user 用户信息
	 * @return accessToken
	 */
	public static TokenInfo createAccessToken(OAuth2User user) {
		Kv kv = Kv.create().set(TOKEN_TYPE, ACCESS_TOKEN)
			.set(CLIENT_ID, user.getClient().getClientId())
			.set(TENANT_ID, user.getTenantId())
			.set(USER_ID, user.getUserId())
			.set(DEPT_ID, user.getDeptId())
			.set(POST_ID, user.getPostId())
			.set(ROLE_ID, user.getRoleId())
			.set(OAUTH_ID, user.getOauthId())
			.set(ACCOUNT, user.getAccount())
			.set(USER_NAME, user.getAccount())
			.set(NICK_NAME, user.getName())
			.set(REAL_NAME, user.getRealName())
			.set(ROLE_NAME, user.getRoleName())
			.set(DETAIL, user.getDetail());
		return createToken(kv, user.getClient().getAccessTokenValidity());
	}

	/**
	 * 创建refreshToken
	 *
	 * @param user 用户信息
	 * @return refreshToken
	 */
	public static TokenInfo createRefreshToken(OAuth2User user) {
		Kv kv = Kv.create().set(TOKEN_TYPE, REFRESH_TOKEN)
			.set(USER_ID, user.getUserId())
			.set(DEPT_ID, user.getDeptId())
			.set(ROLE_ID, user.getRoleId());
		return createToken(kv, user.getClient().getRefreshTokenValidity());
	}


	/**
	 * 创建clientAccessToken
	 *
	 * @param client 客户端信息
	 * @return clientToken
	 */
	public static TokenInfo createClientAccessToken(OAuth2Client client) {
		Kv kv = Kv.create().set(TOKEN_TYPE, CLIENT_ACCESS_TOKEN)
			.set(CLIENT_ID, client.getClientId());
		return createToken(kv, client.getAccessTokenValidity());
	}

	/**
	 * 创建implicitAccessToken
	 *
	 * @param user 用户信息
	 * @return implicitAccessToken
	 */
	public static TokenInfo createImplicitAccessToken(OAuth2User user) {
		Kv kv = Kv.create().set(TOKEN_TYPE, IMPLICIT_ACCESS_TOKEN)
			.set(ACCOUNT, user.getAccount());
		return createToken(kv);
	}


}
